package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.mk.admin.mapper.PhysicalMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.entity.PhysicalEntity;
import com.mk.admin.model.vo.PhysicalVo;
import com.mk.admin.service.PhysicalService;
import com.mk.admin.util.HttpTool;
import com.mk.admin.util.JSONCopy;
import com.mk.admin.util.NumberTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PhysicalServiceImpl implements PhysicalService {
    @Autowired
    private PhysicalMapper physicalMapper;

    @Override
    public ResultDto add(PhysicalVo physicalVo) {
        PhysicalEntity entity = JSONCopy.copy(physicalVo, PhysicalEntity.class);
        entity.setPrice(NumberTool.strToMoney(physicalVo.getPrice() + ""));
        return ResultDto.data(physicalMapper.insert(entity));
    }

    @Override
    public ResultDto update(PhysicalVo physicalVo) {
        PhysicalEntity entity = JSONCopy.copy(physicalVo, PhysicalEntity.class);
        entity.setPrice(NumberTool.strToMoney(physicalVo.getPrice() + ""));
        return ResultDto.data(physicalMapper.updateById(entity));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(physicalMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(physicalMapper.status(id));
    }

    @Override
    public ResultDto select(PhysicalVo physicalVo) {
        PhysicalEntity entity = JSONCopy.copy(physicalVo, PhysicalEntity.class);
        return ResultDto.data(physicalMapper.count(entity), physicalMapper.select(entity));
    }

    @Override
    public ResultDto fresh(PhysicalVo vo) {
        JSONObject jsonObject = HttpTool.postJson("pes_0049", vo);
        return ResultDto.data(jsonObject.get("result"));
    }

    @Override
    public ResultDto yuyue(PhysicalVo physicalVo) {
        JSONObject jsonObject = HttpTool.postJson("pes_00444", physicalVo);
        add(physicalVo);
        return ResultDto.data(jsonObject.get("result"));
    }
}
